import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzModalService } from 'ng-zorro-antd';
import { CustomService } from 'src/app/service/custom.service';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.less']
})
export class ProfileComponent implements OnInit {

  validateForm!: FormGroup;
  data: any = {};

  submitForm() {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    if (this.validateForm.status == "VALID") {
      var data = this.validateForm.value;
      if (this.data['isPhoneAuth'])
        data['phone'] = this.data['phone'];
      if (this.data['isEmailAuth'])
        data['email'] = this.data['email'];
      this.customService.modify(data).subscribe(result => {
        this.modalService.success({
          nzTitle: '信息提示',
          nzContent: '个人资料修改成功!'
        });
      });
    }
  }

  constructor(
    private fb: FormBuilder,
    private modalService: NzModalService,
    private customService: CustomService
  ) { }

  ngOnInit(): void {
    this.customService.profile().subscribe(result => {
      let data = result['data'];
      this.data = data;
      this.validateForm.setValue({
        trueName: data['trueName'],
        phone: data['phone'],
        email: data['email'],
        qq: data['qq'],
        companyName: data['companyName'],
        companyAddress: data['companyAddress'],
        companyIntroduce: data['companyIntroduce'],
        officialSite: data['officialSite'],
      });
      if (data['isPhoneAuth'])
        this.validateForm.controls['phone'].disable({ onlySelf: true });
      if (data['isEmailAuth'])
        this.validateForm.controls['email'].disable({ onlySelf: true });
      if (data['authentication'] === 100)
        this.validateForm.controls['trueName'].disable({ onlySelf: true });
    });
    this.validateForm = this.fb.group({
      trueName: [null, [Validators.required]],
      phone: [null, [Validators.required, Validators.pattern(/^13[0-9]{9}$|14[0-9]{9}$|15[0-9]{9}$|16[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$|19[0-9]{9}$/)]],
      email: [null, [Validators.required, Validators.email]],
      qq: [null],
      companyName: [null],
      companyAddress: [null],
      companyIntroduce: [null],
      officialSite: [null]
    });
  }

}
